package com.wutianyi.module.services.impl;

import java.util.List;

import com.wutianyi.module.commons.Item;
import com.wutianyi.module.commons.PropertyGroup;
import com.wutianyi.module.company.Company;
import com.wutianyi.module.dao.CompanyDao;
import com.wutianyi.module.dao.ItemDao;
import com.wutianyi.module.dao.PropertyGroupDao;
import com.wutianyi.module.services.CompanyServices;
import com.wutianyi.module.services.IdGeneratorServices;

/**
 * @author wutianyi
 * 
 */
public class CompanyServicesImpl implements CompanyServices {
	
	private CompanyDao companyDao;
	private PropertyGroupDao propertyGroupDao;
	private ItemDao itemDao;
	
	/*
	 * 创建公司 (non-Javadoc)
	 * 
	 * @see
	 * com.wutianyi.module.services.CompanyServices#createCompany(java.lang.
	 * String, java.lang.String)
	 */
	public Company createCompany(String name, String description) {
		String id = IdGeneratorServices.uuidKey();
		
		// 创建公司的基本信息，名称和描述
		Company company = new Company(id, name, description);
		// 保存
		companyDao.insertCompany(company);
		// // 设置基本的信息
		// PropertyGroup companyName =
		// PropertyGroup.companyName(company.getId(),
		// name, description);
		//
		// company.addPropertyGroup(companyName);
		//
		// List<PropertyGroup> pgs = company.getPgs();
		// propertyGroupDao.insertPropertyGroups(pgs);
		//
		// for (PropertyGroup pg : pgs) {
		// if (null != pg.getItems()) {
		// itemDao.insertItems(pg.getItems(), pg.getId());
		// }
		// }
		
		return company;
	}
	
	public PropertyGroupDao getPropertyGroupDao() {
		return propertyGroupDao;
	}
	
	public void setPropertyGroupDao(PropertyGroupDao propertyGroupDao) {
		this.propertyGroupDao = propertyGroupDao;
	}
	
	public CompanyDao getCompanyDao() {
		return companyDao;
	}
	
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	
	public ItemDao getItemDao() {
		return itemDao;
	}
	
	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}
	
	@Override
	public Company getCompanyByCId(String companyId) {
		Company company = companyDao.getCompanyByCId(companyId);
		if (null != company) {
			List<PropertyGroup> pgs = propertyGroupDao.listPropertyGroupsByCId(company.getId());
			for (PropertyGroup pg : pgs) {
				pg.setItems(itemDao.listItemsByGId(pg.getId()));
			}
			company.setPgs(pgs);
		}
		
		return company;
	}
	
	@Override
	public void addPropertyGroup(int cId, PropertyGroup pg) {
		pg.setcId(cId);
		propertyGroupDao.insertPropertyGroup(pg);
		itemDao.insertItems(pg.getItems(), pg.getId());
	}
	
	@Override
	public void addPropertyGroupItem(int cId, int gId, Item item) {
		item.setgId(gId);
		itemDao.insertItem(item);
	}
	
	@Override
	public void updatePropertyGroupsOrder(int cId, int[] gIds) {
		
	}
	
	@Override
	public void updatePropertyGroupItemsOrder(int cId, int pId, int[] iIds) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public int updateCompany(String companyId, String name, String nameEn, String description,
			List<PropertyGroup> updatePgs, List<PropertyGroup> addPgs) {
		Company company = getCompanyBaseInfoByCId(companyId);
		
		company.setName(name);
		company.setNameEn(nameEn);
		company.setDescription(description);
		companyDao.updateCompany(company);
		addPropertyGroups(company.getId(), addPgs);
		updatePropertyGroups(company.getId(), updatePgs);
		
		return company.getId();
	}
	
	@Override
	public void addPropertyGroups(int cId, List<PropertyGroup> pgs) {
		if (null == pgs || pgs.isEmpty()) {
			return;
		}
		
		for (PropertyGroup pg : pgs) {
			addPropertyGroup(cId, pg);
		}
		
	}
	
	@Override
	public void updatePropertyGroups(int cId, List<PropertyGroup> pgs) {
		if (null == pgs || pgs.isEmpty()) {
			return;
		}
		for (PropertyGroup pg : pgs) {
			pg.setcId(cId);
			int r = propertyGroupDao.updatePropertyGroup(pg);
			if (r != 0) {
				itemDao.updateItems(pg.getItems());
			}
		}
	}
	
	@Override
	public Company getCompanyBaseInfoByCId(String companyId) {
		return companyDao.getCompanyBaseInfoByCId(companyId);
	}
	
	@Override
	public List<Company> listCompanys() {
		return companyDao.listCompanys();
	}
	
	@Override
	public Company getCompanyById(int id) {
		Company company = companyDao.getCompanyById(id);
		if (null != company) {
			List<PropertyGroup> pgs = propertyGroupDao.listPropertyGroupsByCId(company.getId());
			for (PropertyGroup pg : pgs) {
				pg.setItems(itemDao.listItemsByGId(pg.getId()));
			}
			company.setPgs(pgs);
		}
		return company;
	}
	
}
